#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

# gae imports
from google.appengine.ext import db


class DatastoreModel(db.Model):
    """
    GAEO enhanced model in Datastore implementation
    """
    
    def before_put(self):
        pass
    
    def after_put(self):
        pass
    
    def put(self, **kwargs):
        if self.before_put() is not False:
            if kwargs:
                key = super(DatastoreModel, self).put(**kwargs)
            else:
                key = super(DatastoreModel, self).put()
            self.after_put()
            return key
    
    # alias
    update = put
    
    def set_attributes(self, **attrs):
        props = self.properties()
        for prop in props.values():
            if prop.name in attrs:
                prop.__set__(self, attrs[prop.name])
    
    def update_attributes(self, **attrs):
        self.set_attributes(**attrs)
        self.update()